package com.api.base.service.impl;

import com.api.base.dao.UserMapper;
import com.api.base.dao.VerificationCodeMapper;
import com.api.base.model.VerificationCode;
import com.api.base.service.VerificationCodeService;
import com.api.common.EmailUtil;
import com.api.core.service.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/04/11.
 */
@Service
@Transactional
public class VerificationCodeServiceImpl extends AbstractService<VerificationCode> implements VerificationCodeService {
    @Resource
    private VerificationCodeMapper verificationCodeMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Result list(String search, String order, Integer page, Integer size){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> res = verificationCodeMapper.baseList(params, orderParams);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result send(VerificationCode verificationCode) {
        VerificationCode old =  verificationCodeMapper.isExists(verificationCode.getEmail(),verificationCode.getType());
        if(old==null){
            save(verificationCode);
        }
        if(old!=null){
            //过期删掉
            deleteById(old);
            save(verificationCode);
        }
        // 发送邮箱
        String email = "<p>You recently requested for a verification code for your BOUNTEE account. </p>" +
                "<br/>"+
                "<p>Enter your 4-digit BOUNTEE verification code shown below to proceed.</p>" +
                "<br/>"+
                "<h2>"+verificationCode.getVerificationCode()+"<h2/>" +
                "<br/>"+
                "<br/>"+
                "<br/>"+
                "<p>This code is valid for 5 minutes and usable only once.</p>"+
                "<p>Thank you,</p>"+
                "<p>BOUNTEE _ team</p>";
        EmailUtil.gmailSender(verificationCode.getEmail(),email,"Bountee Email Verification Code");
        return ResultGenerator.genSuccessResult();
    }
}
